import {createRouter, createWebHistory, createWebHashHistory} from 'vue-router'

import {getToken} from "../api/token.js";
import store from "../store";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/",
            name: "index",
            component: () => import("../views/IndexView.vue")
        },
        {
            path: "/sysView",
            name: "sys",
            component: () => import("../views/SystemView.vue")
        },
        {
            path: '/menuView',
            name: 'menu',
            component: () => import('../views/MenuView.vue')
        },
        {
            path: '/funcView',
            name: 'func',
            component: () => import('../views/FuncView.vue')
        },
        {
            path: '/orgView',
            name: 'org',
            component: () => import('../views/OrganizationView.vue')
        },
        {
            path: '/positionView',
            name: 'job',
            component: () => import('../views/PositionView.vue')
        },
        {
            path: '/userView',
            name: 'user',
            component: () => import('../views/UserView.vue')
        },
        {
            path: '/roleView',
            name: 'role',
            component: () => import('../views/RoleView.vue')
        },
        {
            path: '/log/loginView',
            name: 'loginLog',
            component: () => import('../views/LoginLogView.vue')
        },
        {
            path: '/log/serviceView',
            name: 'errorLog',
            component: () => import('../views/ServerLogView.vue')
        },
        {
            path: "/swiperView",
            name: "swiper",
            component: () => import("../views/SwiperView.vue")
        },
        {
            path: "/advertView",
            name: "advert" ,
            component : () => import("../views/AdvertView.vue")
        },
        {
            path: '/svipConfigView',
            name: 'svipConfig',
            component: () => import('../views/VipConfigView.vue')
        },
        {
            path: "/resourceTypeView",
            name: "resourceType",
            component: () => import("../views/ResourceTypeView.vue")
        },
        {
            path: "/resourceView",
            name: "resource",
            component: () => import("../views/ResourceView.vue"),
            props:(route) => {  // 将状态 通过 props 进行绑定，实现 组件和 router的解耦
                return {
                    state: route.query.state,
                    action: route.query.a
                }
            }
        },
        {
            path: '/:catchAll(.*)',
            name: 'notFound',
            component: () => import('../views/404.vue')
        }
    ]
})

// 绑定 全局前置路由守卫，验证登录权限
/**
 *  to, from 均代表 route 路由对象
 *    to : 去到哪个 route 路由中
 *    from : 从 哪个 route 路由中来
 *
 *   next 是一个函数 ，可以实现 跳转到指定路由 next({name: "xxx"})，阻止路由条件 next(false)， 继续向下执行 next()
 */

router.beforeEach(async (to, from, next) => {
    if (to.path === "/login") { next(); return }
    try{
        let accessToken =  await getToken();
        if (accessToken == null) {
            next("/login");
        }else{
            next();
        }
    }catch (e) {
        next("/login"); // 当接口调用出现异常，跳转到登录
    }

})


export default router
